package doantotnghiep.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.DivisionRoomBusiness;
import doantotnghiep.business.StaffBusiness;
import doantotnghiep.model.DivisionRoom;
import doantotnghiep.model.Staff;
import doantotnghiep.model.WorkPlayment;
import doantotnghiep.modelDTO.DivisionRoomDTO;
import doantotnghiep.modelDTO.StaffDTO;
import doantotnghiep.modelDTO.WorkPlaymentDTO;

/**				
* StaffManagController			
*				
* Version 1.0				
*				
* Date: 07-04-2014			
*				
* Copyright 				
*				
* Modification Logs:				
* DATE                 AUTHOR          DESCRIPTION				
* -----------------------------------------------------------------------				
* 07-04-2014	        TamNV            Create				
*/			
@Controller
public class StaffManagController {

	@Autowired
	private StaffBusiness staffBusiness;
	
	@Autowired
	private DivisionRoomBusiness divisionRoomBusiness;
	
	private Staff staff;
	
	private StaffDTO staffDTO;
	
	private static int curentRoomID;
	
	@ModelAttribute("StaffDTO")
	public StaffDTO getStaffObject() {
		return new StaffDTO();
	}
    
	/*receive require view all staff from client*/
	@RequestMapping(value = "/viewStaff", method = RequestMethod.GET)
	public ModelAndView viewAllStaff() {
		List<Staff> staffs;
		List<StaffDTO> staffDTOs = new ArrayList<StaffDTO>();
		if(staffBusiness != null){
			
			//get a list of staff from database
			staffs = staffBusiness.listStaff();
			if(staffs != null){
				
				//transfering each staff object into staffdto object to display in jsp file
				for (Staff c : staffs) {
					StaffDTO staffDTO = new StaffDTO();
					
					staffDTO.setId(c.getId());//id
					staffDTO.setFullName(c.getFullName());//full name
					staffDTO.setIdentifyNumber(c.getIdentifyNumber());// identifyCard  Number
					staffDTO.setBirthDay(c.getBirthDay());
					staffDTO.setAddress(c.getAddress());
					staffDTO.setTelNumber(c.getTelNumber());					
					staffDTO.setEmail(c.getEmail());
					staffDTO.setIncome(c.getIncome());
					staffDTO.setSex(c.getSex());
					staffDTO.setPositionName(c.getPosition().getWorkPosition());
					staffDTO.setRoomName(c.getDivisionRoom().getRoomName());
					
					staffDTOs.add(staffDTO);
				}	
			}
			
			/*put data into a Map object to return for jsp*/
			Map<String, Object> staffModel = new HashMap<String, Object>();
			staffModel.put("staffDTOs", staffDTOs);
			return new ModelAndView("viewStaff", "staffModel",staffModel);
		}
		return new ModelAndView("viewStaff","",null);
	}

	/*receive require add a new staff from client*/
	@RequestMapping(value = "/addStaff", method = RequestMethod.GET)
	public ModelAndView addStaff() {
		List<DivisionRoomDTO> listDivisionRoomDTOs = new ArrayList<DivisionRoomDTO>(); 
		
		//get list of division room from database 
		List<DivisionRoom> listdivisionRoom = divisionRoomBusiness.getListValidRoom();
		
		//transfering into DivisionRoomDTO to display
		for(DivisionRoom c:listdivisionRoom){
			DivisionRoomDTO divisionRoomDTO = new DivisionRoomDTO();
			divisionRoomDTO.setId(c.getId());
			
			divisionRoomDTO.setRoomName(c.getRoomName());
			listDivisionRoomDTOs.add(divisionRoomDTO);
		}
		
		List<WorkPlaymentDTO> listPosition = new ArrayList<WorkPlaymentDTO>();
		List<WorkPlayment> works = staffBusiness.getAllValidPosition();
		
		for(WorkPlayment position: works){
			WorkPlaymentDTO work = new WorkPlaymentDTO();
			work.setId(position.getId());
			work.setPosition(position.getWorkPosition());
			listPosition.add(work);
		}
		
		Map<String, Object> staffModel = new HashMap<String, Object>();
		staffModel.put("listRoom", listDivisionRoomDTOs);
		staffModel.put("positions",listPosition);
	    return new ModelAndView("addStaff", "staffModel", staffModel);
	}
	
	/*receive require add a new staff from client after they entered all input*/
	@RequestMapping(value = "/addStaff", method = RequestMethod.POST)
	public String addStaff(HttpServletRequest request, ModelMap model,
			@ModelAttribute("StaffDTO") StaffDTO staffDTO) {
		if (staffDTO != null) {
			
			//tranfering into Staff Object to add into database
			staff = new Staff();
			staff.setFullName(staffDTO.getFullName());
			staff.setBirthDay(staffDTO.getBirthDay());
			staff.setIdentifyNumber(staffDTO.getIdentifyNumber());
			staff.setEmail(staffDTO.getEmail());
			staff.setAddress(staffDTO.getAddress());
			staff.setIncome(staffDTO.getIncome());
			staff.setSex(staffDTO.getSex());
			staff.setTelNumber(staffDTO.getTelNumber());
			
			//set work position
			WorkPlayment playment = new WorkPlayment();
			playment.setId(staffDTO.getPositionID());
			staff.setPosition(playment);

			//set Room for staff
			DivisionRoom divisionRoom = new DivisionRoom();
			divisionRoom.setId(staffDTO.getRoom_ID());
			staff.setDivisionRoom(divisionRoom);
			
			divisionRoom.setNumberOfStaff(divisionRoom.getNumberOfStaff()+1);
			
			this.staffBusiness.addStaff(staff);
		} 
		return "redirect:/viewStaff";
	}

	/*receive require update information of a staff from client*/
	@RequestMapping(value = "/editStaff", method = RequestMethod.GET)
	public ModelAndView editStaff(HttpServletRequest request, ModelMap model,int id) {
		Staff staff = staffBusiness.getStaffById(id);
		if(staff != null){
			
			//transfering into a StaffDTO object to display
			staffDTO = new StaffDTO();
			staffDTO.setId(staff.getId());
			staffDTO.setBirthDay(staff.getBirthDay());
			staffDTO.setIdentifyNumber(staff.getIdentifyNumber());
			staffDTO.setAddress(staff.getAddress());
			staffDTO.setEmail(staff.getEmail());
			staffDTO.setFullName(staff.getFullName());
			staffDTO.setIncome(staff.getIncome());
			staffDTO.setSex(staff.getSex());
			staffDTO.setTelNumber(staff.getTelNumber());
			curentRoomID = staff.getDivisionRoom().getId();
			staffDTO.setRoom_ID(curentRoomID);
			staffDTO.setPositionID(staff.getPosition().getId());
			
			/*get all division room for client select in jsp form*/
			List<DivisionRoomDTO> listDivisionRoomDTOs = new ArrayList<DivisionRoomDTO>();
			List<DivisionRoom> listdivisionRoom = divisionRoomBusiness.getListRoom();
			
			for(DivisionRoom c:listdivisionRoom){
				DivisionRoomDTO divisionRoomDTO = new DivisionRoomDTO();
				divisionRoomDTO.setId(c.getId());
				divisionRoomDTO.setRoomName(c.getRoomName());
				listDivisionRoomDTOs.add(divisionRoomDTO);
			}
			
			List<WorkPlaymentDTO> listPosition = new ArrayList<WorkPlaymentDTO>();
			List<WorkPlayment> works = staffBusiness.getAllPosition();
			
			for(WorkPlayment position:works){
				WorkPlaymentDTO work= new WorkPlaymentDTO();
				work.setId(position.getId());
				work.setPosition(position.getWorkPosition());
				listPosition.add(work);				
			}
			
			/*put data into a Map object to return */
			Map<String, Object> staffModel = new HashMap<String, Object>();
			staffModel.put("listDivisionRoomDTO", listDivisionRoomDTOs);
			staffModel.put("staffDTO",staffDTO);
			staffModel.put("positions",listPosition);
		    return new ModelAndView("updateStaff", "staffModel", staffModel);
		}
		return new ModelAndView("viewStaff","",null);
	}

    /*receive require update infor of a staff from client after them entered all input*/
	@RequestMapping(value = "/editStaff", method = RequestMethod.POST)
	public String editStaff(HttpServletRequest request, ModelMap model,
			@ModelAttribute("StaffDTO") StaffDTO staffDTO) {
		if (staffDTO != null) {
			
			//transfering to Staff object to save into database
			staff = new Staff();
			
			staff.setId(staffDTO.getId());
			staff.setBirthDay(staffDTO.getBirthDay());
			staff.setIdentifyNumber(staffDTO.getIdentifyNumber());
			staff.setAddress(staffDTO.getAddress());
			staff.setEmail(staffDTO.getEmail());
			staff.setFullName(staffDTO.getFullName());
			staff.setIncome(staffDTO.getIncome());
			
			WorkPlayment playment = new WorkPlayment();
			playment.setId(staffDTO.getPositionID());
			
			staff.setPosition(playment);
			staff.setSex(staffDTO.getSex());
			staff.setTelNumber(staffDTO.getTelNumber());
			
			DivisionRoom divisionRoom = new DivisionRoom();
			divisionRoom.setId(staffDTO.getRoom_ID());
			
			staff.setDivisionRoom(divisionRoom);
			if(staffDTO.getRoom_ID()!= curentRoomID){
				divisionRoom.setNumberOfStaff(divisionRoom.getNumberOfStaff()+1);//increase number staff of new room by 1
				divisionRoomBusiness.saveChanged(divisionRoom);
				DivisionRoom oldRoom = divisionRoomBusiness.getRoomByID(curentRoomID);//decrease number staff of old room by 1
				oldRoom.setNumberOfStaff(oldRoom.getNumberOfStaff()-1);
			}
			this.staffBusiness.saveStaffChanged(staff);
		}
		return "redirect:/viewStaff";
	}

	/*receive require delete a staff from database*/
	@RequestMapping(value = "/deleteStaff", method = RequestMethod.GET)
	public String deleteStaff(HttpServletRequest request, ModelMap model,
			  int id) {
		System.out.println("delete that");
		staffBusiness.deleteStaff(id);
		return "redirect:/viewStaff";
	}
	
	/*receive require search a staff from database*/
	@RequestMapping(value = "/searchStaff", method = RequestMethod.GET)
	public ModelAndView searchStaff(HttpServletRequest request, ModelMap model,
			  String key) {
		byte[] tmp = key.getBytes();
		String value="";
		try {
			value = new String(tmp,"UTF-8");
			request.setCharacterEncoding("UTF-8");
			 
			System.out.println(value);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<Staff> staffs;
		Map<String, Object> staffModel = new HashMap<String, Object>();
		List<StaffDTO> staffDTOs = new ArrayList<StaffDTO>();
		staffs = staffBusiness.getStaffByKey(value);

		if(staffs!=null){
			for(Staff c: staffs){
				StaffDTO staffDTO = new StaffDTO();
				
				staffDTO.setId(c.getId());
				staffDTO.setFullName(c.getFullName());
				staffDTO.setBirthDay(c.getBirthDay());
				staffDTO.setIdentifyNumber(c.getIdentifyNumber());
				staffDTO.setAddress(c.getAddress());
				staffDTO.setEmail(c.getEmail());
				staffDTO.setIncome(c.getIncome());
				staffDTO.setPositionName(c.getPosition().getWorkPosition());
				staffDTO.setRoomName(c.getDivisionRoom().getRoomName());
				staffDTO.setSex(c.getSex());
				staffDTO.setTelNumber(c.getTelNumber());
				
				staffDTOs.add(staffDTO);	
			}
			
			staffModel.put("staffDTOs",staffDTOs);
		}
		return new ModelAndView("staffSearchResult","staffModel",staffModel);
	}
}
